Skip to content

fix: never crash on output the console's encoding cannot represent - #104

Merged
JPHutchins merged 1 commit into
intercreate:mainfrom
Filip-Dymczyk:work/charmap_error_fix
Nov 11, 2026
Merged

JPHutchins merged 1 commit into
intercreate:mainfrom
Filip-Dymczyk:work/charmap_error_fix

Conversation

@Filip-Dymczyk

@Filip-Dymczyk Filip-Dymczyk commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Warning

LLM Disclosure

This description was rewritten by claude-opus-5-5 on behalf of @JPHutchins, who asked me to review this PR and get it across the finish line. @Filip-Dymczyk's original report is kept below.

Context (from @Filip-Dymczyk): wrapping the downloaded .exe in Python crashed with UnicodeEncodeError: 'charmap' codec can't encode character '⠦'. Rich's braille spinner can't be written to a cp1252 stdout, which is what a Windows pipe uses. The same crash was reported in #91.

Changes

  • Writes never fail on encoding. replace_unencodable_output(), called first in the CLI callback, sets stdout and stderr to write ? for any character they can't encode, instead of raising. This covers every output path: spinners, task descriptions holding non-Latin addresses or paths, the • in upload bars on ascii/cp437, and Rich tracebacks.
  • A spinner that still draws. spinner_progress() builds all three spinners (connect, request, file download). It keeps dots, but switches to Rich's ASCII line spinner where Rich's own progress bar falls back to ASCII (legacy_windows or ascii_only). It uses Rich's global console, so log lines still render above the spinner.
Verification

With PYTHONIOENCODING=cp1252 and stdout piped:

main this PR
--port /dev/does-not-exist os echo hi UnicodeEncodeError … '⠴' / Connecting to /dev/does-not-exist... error, exit 1
--port /dev/☃ os echo hi UnicodeEncodeError … '☃' / Connecting to /dev/?... error, exit 1
  • test_cp1252_output_replaces_what_it_cannot_encode drives the CLI with a cp1252 stdout. With replace_unencodable_output stubbed out, the same invocation raises UnicodeEncodeError.
  • tests/test_spinner_progress.py checks for ASCII-only output on cp1252, ascii and legacy Windows consoles, and braille kept on UTF-8.
  • black, isort, flake8, mypy and pytest (21 passed) all pass.

PEP 686 makes UTF-8 the default in Python 3.15, but the executables are still built on 3.12.

Closes #91 (already closed; same root cause).

🤖 Generated with Claude Code

@JPHutchins

JPHutchins commented Aug 13, 2026 •

Copy link
Copy Markdown
Collaborator

I like this, makes sense that Rich would have thought of this.

Related to: #91

FYI, smpmgr is not really meant to be wrapped, since it is a user-facing CLI app. I highly recommend writing your own app with smpclient at the point you need reliability, performance, customization, etc.

https://rich.readthedocs.io/en/stable/reference/console.html#rich.console.Console

Please run the linters locally, rebase, and squash.

. ./envr.ps1
lint

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates CLI progress spinners to avoid Windows charmap encoding failures.

Changes:

  • Uses an ASCII-compatible spinner.
  • Forces terminal rendering only when stdout is a terminal.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread smpmgr/common.py Resolved
@Filip-Dymczyk
Filip-Dymczyk force-pushed the work/charmap_error_fix branch 2 times, most recently from 79a766a to 23fd29c Compare August 14, 2026 15:58
@JPHutchins

Copy link
Copy Markdown
Collaborator

@Filip-Dymczyk Sorry that this fell off my radar! Doing some updates to this branch now and getting it merged + release ASAP. Thanks!

@JPHutchins
JPHutchins force-pushed the work/charmap_error_fix branch from 23fd29c to df1e6ec Compare November 11, 2026 00:58
JPHutchins added a commit to Filip-Dymczyk/smpmgr that referenced this pull request Nov 11, 2026
Rich's default spinner ("dots") is drawn in braille (U+2800-U+28FF). When
stdout's encoding cannot represent it, writing a frame raises and the
command aborts. That happens on a Windows console with the cp1252 code page
(intercreate#91), or on the pipe that a process wrapping the smpmgr executable reads
(intercreate#104):

    UnicodeEncodeError: 'charmap' codec can't encode character '⠴'
    in position 0: character maps to <undefined>

spinner_progress() now builds every spinner Progress: connect_with_spinner,
smp_request, and `file download`, which intercreate#104 missed. It keeps "dots" on a
UTF console and switches to Rich's ASCII "line" spinner when
`Console.options.ascii_only` is true (the encoding is not utf-*). That is
the same test Rich's own ProgressBar uses for its ASCII fallback. It still
uses Rich's global console, so RichHandler log records keep rendering
above the live spinner, and FORCE_COLOR / TTY_COMPATIBLE detection is
unchanged.

Compared with intercreate#104 as first proposed: that version forced the "line"
spinner on everyone and gave each Progress a private
`Console(force_terminal=sys.stdout.isatty())`. The private console
separated the spinner from the console RichHandler logs through, so log
lines collide with the spinner. force_terminal overrode Rich's environment
detection, and `sys.stdout.isatty()` raises when sys.stdout is None. Its
unrelated black reformat of main.py is dropped.

PEP 686 makes UTF-8 mode the default in Python 3.15, but the executables
are still built on 3.12 (build.yaml), so this still matters.

Verified: with PYTHONIOENCODING=cp1252 and stdout piped,
`smpmgr --port /dev/does-not-exist os echo hi` crashes on main with the
error above. With this change it prints
"/ Connecting to /dev/does-not-exist... error" and exits 1.
tests/test_spinner_progress.py shows Rich's default spinner raising on
cp1252, ASCII-only output on cp1252 and ascii, and braille kept on UTF-8.

Co-authored-by: Filip <filip.dymczyk@o2.pl>
Co-Authored-By: claude-opus-5-5 <noreply@anthropic.com>
@JPHutchins JPHutchins changed the title fix: charmap error fix: draw the spinner in ASCII when the console can't encode braille Nov 11, 2026
@JPHutchins

Copy link
Copy Markdown
Collaborator

Warning

LLM Disclosure

This post was authored by claude-opus-5-5 on behalf of @JPHutchins. JP asked me to review this PR and get it across the finish line.

Thanks @Filip-Dymczyk for tracking this down. I rebased onto main and squashed this to one commit (df1e6ec); it credits you with Co-authored-by. The approach changed after review, so here's what changed and why:

  • One helper, spinner_progress() in common.py, now builds every spinner, including file download, which still used the braille spinner.
  • It switches to the ASCII line spinner only when the console's encoding isn't UTF (console.options.ascii_only), the same check Rich's own progress bars use for their ASCII fallback. UTF-8 terminals keep the dots spinner.
  • No private Console(force_terminal=sys.stdout.isatty()). A separate console lets RichHandler log lines collide with the live spinner, overrides Rich's FORCE_COLOR / TTY_COMPATIBLE detection, and raises when sys.stdout is None. The line spinner did the actual fixing.
  • Dropped the unrelated main.py reformat.
  • Added tests/test_spinner_progress.py. Rich's default spinner raises on a cp1252 stream; the new one writes only ASCII on cp1252 and ascii and keeps braille on UTF-8.
Reproduction

With PYTHONIOENCODING=cp1252 and stdout piped, smpmgr --port /dev/does-not-exist os echo hi:

  • main fails with: UnicodeEncodeError: 'charmap' codec can't encode character '⠴' in position 0: character maps to <undefined>
  • this PR prints / Connecting to /dev/does-not-exist... error and exits 1.

Locally, black, isort, flake8, mypy and pytest (20 passed) all pass.

This is also #91. PEP 686 makes UTF-8 the default in Python 3.15, but the executables are still built on 3.12, so the fix still matters.

When stdout's encoding cannot represent a character smpmgr writes,
Python raises and the command aborts. That happens on a Windows console
with the cp1252 code page (intercreate#91), or on the pipe that a process wrapping
the smpmgr executable reads (intercreate#104):

    UnicodeEncodeError: 'charmap' codec can't encode character '⠴'
    in position 0: character maps to <undefined>

The first character to fail is Rich's braille spinner, but it is not the
only one. A transport address, BLE name or file path in a task
description can fail too (`--port /dev/☃` on cp1252), as can the "•"
separator of the upload progress bars on ascii/cp437, and Rich
tracebacks.

Two layers:

- replace_unencodable_output(), called first in the CLI callback,
  reconfigures stdout and stderr with errors="replace". A character the
  stream cannot encode is written as "?" instead of raising, on every
  output path at once.
- spinner_progress() builds every spinner Progress: connect_with_spinner,
  smp_request, and `file download`, which intercreate#104 missed. It keeps "dots",
  but draws Rich's ASCII "line" spinner where Rich's own ProgressBar
  falls back to ASCII (`legacy_windows or options.ascii_only`, rich
  progress_bar.py). The spinner then animates instead of degrading to
  "?". It takes Rich's global console, so RichHandler log records still
  render above the live spinner, and FORCE_COLOR / TTY_COMPATIBLE
  detection is unchanged.

Compared with intercreate#104 as first proposed: that version forced the "line"
spinner on everyone and gave each Progress a private
`Console(force_terminal=sys.stdout.isatty())`. The private console
separated the spinner from the console RichHandler logs through, so log
lines collide with the spinner. force_terminal overrode Rich's
environment detection, and `sys.stdout.isatty()` raises when sys.stdout
is None. Its unrelated black reformat of main.py is dropped.

PEP 686 makes UTF-8 mode the default in Python 3.15, but the executables
are still built on 3.12 (build.yaml), so this still matters. Building them
in UTF-8 mode would be the other route.

Verified, with PYTHONIOENCODING=cp1252 and stdout piped:
`smpmgr --port /dev/does-not-exist os echo hi` crashes on main with the
error above, and `--port /dev/☃` crashes on '☃'. With this change
they print "/ Connecting to /dev/does-not-exist... error" and
"/ Connecting to /dev/?... error", and exit 1. In tests:
- test_cp1252_output_replaces_what_it_cannot_encode drives the CLI with
  a cp1252 stdout. With replace_unencodable_output stubbed out, the same
  invocation raises UnicodeEncodeError.
- tests/test_spinner_progress.py checks for ASCII-only output on cp1252,
  ascii and legacy Windows consoles, and braille kept on UTF-8.

Co-authored-by: Filip <filip.dymczyk@o2.pl>
Co-Authored-By: claude-opus-5-5 <noreply@anthropic.com>
@JPHutchins
JPHutchins force-pushed the work/charmap_error_fix branch from df1e6ec to cbccdd3 Compare November 11, 2026 02:58
@JPHutchins JPHutchins changed the title fix: draw the spinner in ASCII when the console can't encode braille fix: never crash on output the console's encoding cannot represent Nov 11, 2026
@JPHutchins
JPHutchins merged commit b65a667 into intercreate:main Nov 11, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnicodeEncodeError crash on Windows (cp1252) from Rich spinner characters

3 participants